package cn.tedu.weibo1.controller;


import cn.tedu.weibo1.base.response.JsonResult;
import cn.tedu.weibo1.mapper.WeiboMapper;
import cn.tedu.weibo1.pojo.entity.Weibo;
import cn.tedu.weibo1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;

@Slf4j
@RestController
@RequestMapping("v1/weibo/")
@Api(tags = "微博模块")
@Validated
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;


    @PostMapping("insert")
    @ApiOperation("发布微博")
    public JsonResult insertWeibo(@RequestBody @NotBlank(message = "content不允许为空") String content, @ApiIgnore HttpSession session){
        log.debug("content:"+content);
        UserVO userVO=(UserVO) session.getAttribute("user");
        Weibo weibo=new Weibo();
        weibo.setContent(content);
        weibo.setUserId(userVO.getId());
        int i = weiboMapper.insertWeibo(weibo);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }



    @GetMapping("Allweibo")
    @ApiOperation("获取所有微博")
    public JsonResult selectAllWeibo(){
        return JsonResult.ok(weiboMapper.selectALlWeibo());
    }


    @GetMapping("selectById")
    @ApiOperation("微博详情")
    public JsonResult selectWeiboById(Long id){
        return JsonResult.ok(weiboMapper.selectWeiboById(id));
    }



}
